<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>我的课程 - AI智能教学培训系统</title>
    <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
    <script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.js"></script>
    <script src="https://unpkg.com/element-ui/lib/index.js"></script>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: 'Segoe UI', 'Microsoft YaHei', sans-serif;
        }

        :root {
            --primary: #4361ee;
            --secondary: #3f37c9;
            --accent: #4895ef;
            --light: #f8f9fa;
            --dark: #212529;
            --success: #4cc9f0;
            --warning: #f72585;
            --gray: #6c757d;
        }

        body {
            background-color: #f5f7fb;
            color: var(--dark);
        }

        .app-container {
            display: flex;
            min-height: 100vh;
        }

        /* 侧边栏样式 */
        .sidebar {
            width: 260px;
            background: linear-gradient(180deg, var(--primary), var(--secondary));
            color: white;
            box-shadow: 3px 0 10px rgba(0, 0, 0, 0.1);
            transition: all 0.3s;
            position: fixed;
            height: 100vh;
            overflow-y: auto;
        }

        .logo-container {
            padding: 20px;
            text-align: center;
            border-bottom: 1px solid rgba(255, 255, 255, 0.1);
        }

        .logo {
            font-size: 24px;
            font-weight: 700;
            display: flex;
            align-items: center;
            justify-content: center;
        }

        .logo-icon {
            margin-right: 10px;
            font-size: 28px;
        }

        .nav-menu {
            padding: 20px 0;
        }

        .nav-item {
            padding: 12px 25px;
            display: flex;
            align-items: center;
            cursor: pointer;
            transition: all 0.3s;
            border-left: 4px solid transparent;
        }

        .nav-item:hover {
            background: rgba(255, 255, 255, 0.1);
            border-left: 4px solid var(--accent);
        }

        .nav-item.active {
            background: rgba(255, 255, 255, 0.1);
            border-left: 4px solid var(--accent);
        }

        /* 侧边栏链接无下划线且不变色 */
        a.nav-item {
            text-decoration: none;
            color: inherit;
        }
        a.nav-item:visited,
        a.nav-item:active,
        a.nav-item:focus {
            color: inherit;
        }

        .nav-icon {
            margin-right: 12px;
            font-size: 18px;
        }

        /* 主内容区 */
        .main-content {
            flex: 1;
            margin-left: 260px;
            padding: 30px;
            transition: all 0.3s;
        }

        .header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 30px;
        }

        .page-title {
            font-size: 28px;
            font-weight: 600;
            color: var(--dark);
        }

        .user-info {
            display: flex;
            align-items: center;
        }

        .avatar {
            width: 40px;
            height: 40px;
            border-radius: 50%;
            background: var(--accent);
            display: flex;
            align-items: center;
            justify-content: center;
            color: white;
            font-weight: bold;
            margin-right: 10px;
        }

        /* 课程页面特定样式 */
        .filters {
            display: flex;
            justify-content: space-between;
            margin-bottom: 25px;
            align-items: center;
        }

        .search-input {
            width: 300px;
        }

        .filter-tabs {
            display: flex;
            background: #f0f2f5;
            border-radius: 8px;
            padding: 5px;
        }

        .filter-tab {
            padding: 8px 20px;
            border-radius: 6px;
            cursor: pointer;
            font-size: 14px;
            transition: all 0.3s;
        }

        .filter-tab.active {
            background: white;
            box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
            font-weight: 500;
            color: var(--primary);
        }

        .courses-container {
            display: grid;
            grid-template-columns: repeat(auto-fill, minmax(320px, 1fr));
            gap: 25px;
        }

        .course-card {
            background: white;
            border-radius: 12px;
            overflow: hidden;
            box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05);
            transition: all 0.3s;
        }

        .course-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 8px 20px rgba(0, 0, 0, 0.1);
        }

        .course-header {
            position: relative;
            height: 160px;
            background: linear-gradient(135deg, var(--primary), var(--accent));
        }

        .course-tag {
            position: absolute;
            top: 15px;
            right: 15px;
            background: white;
            color: var(--primary);
            padding: 4px 10px;
            border-radius: 20px;
            font-size: 12px;
            font-weight: 500;
        }

        .course-body {
            padding: 20px;
        }

        .course-title {
            font-size: 18px;
            font-weight: 600;
            margin-bottom: 10px;
            display: flex;
            justify-content: space-between;
        }

        .course-instructor {
            color: var(--gray);
            font-size: 14px;
            margin-bottom: 15px;
        }

        .course-progress {
            margin-bottom: 15px;
        }

        .progress-label {
            display: flex;
            justify-content: space-between;
            margin-bottom: 8px;
            font-size: 14px;
        }

        .progress-bar {
            height: 8px;
            background: #e9ecef;
            border-radius: 4px;
            overflow: hidden;
        }

        .progress-fill {
            height: 100%;
            background: var(--success);
            border-radius: 4px;
        }

        .course-stats {
            display: flex;
            justify-content: space-between;
            color: var(--gray);
            font-size: 14px;
            margin-top: 15px;
        }

        .course-actions {
            display: flex;
            justify-content: space-between;
            margin-top: 20px;
        }

        .action-btn {
            flex: 1;
            padding: 10px;
            border: none;
            background: #f8f9fa;
            color: var(--dark);
            cursor: pointer;
            transition: all 0.3s;
            font-size: 14px;
            display: flex;
            align-items: center;
            justify-content: center;
        }

        .action-btn:first-child {
            border-radius: 6px 0 0 6px;
            border-right: 1px solid #eee;
        }

        .action-btn:last-child {
            border-radius: 0 6px 6px 0;
        }

        .action-btn:hover {
            background: #e9ecef;
        }

        .action-btn.primary {
            background: var(--primary);
            color: white;
        }

        .action-btn.primary:hover {
            background: var(--secondary);
        }

        .action-icon {
            margin-right: 6px;
        }

        .empty-state {
            text-align: center;
            padding: 50px 20px;
            grid-column: 1 / -1;
        }

        .empty-icon {
            font-size: 60px;
            color: #dee2e6;
            margin-bottom: 20px;
        }

        .empty-text {
            font-size: 18px;
            color: var(--gray);
            margin-bottom: 20px;
        }

        /* 响应式设计 */
        @media (max-width: 992px) {
            .sidebar {
                width: 70px;
            }

            .logo-text, .nav-text {
                display: none;
            }

            .logo-icon {
                margin-right: 0;
            }

            .nav-item {
                justify-content: center;
            }

            .nav-icon {
                margin-right: 0;
            }

            .main-content {
                margin-left: 70px;
            }
        }

        @media (max-width: 768px) {
            .filters {
                flex-direction: column;
                align-items: flex-start;
                gap: 15px;
            }

            .search-input {
                width: 100%;
            }

            .courses-container {
                grid-template-columns: 1fr;
            }
        }

        /* 动画效果 */
        @keyframes fadeIn {
            from { opacity: 0; transform: translateY(10px); }
            to { opacity: 1; transform: translateY(0); }
        }

        .fade-in {
            animation: fadeIn 0.5s ease forwards;
        }
    </style>
</head>
<body>
<div id="app">
    <div class="app-container">
        <!-- 侧边导航 -->
        <div id="sidebar"></div>
        <!-- 主内容区 -->
        <div class="main-content">
            <!-- 顶部导航 -->
            <div class="header">
                <h1 class="page-title">我的课程</h1>
                <div class="user-info">
                    <div class="avatar">张</div>
                    <div>
                        <div class="user-name">张明</div>
                        <div class="user-role">学生</div>
                    </div>
                </div>
            </div>

            <!-- 筛选区域 -->
            <div class="filters">
                <el-input
                        placeholder="搜索课程名称或教师"
                        prefix-icon="el-icon-search"
                        v-model="searchQuery"
                        class="search-input">
                </el-input>

                <div class="filter-tabs">
                    <div
                            class="filter-tab"
                            :class="{active: activeFilter === 'all'}"
                            @click="activeFilter = 'all'">
                        全部课程
                    </div>
                    <div
                            class="filter-tab"
                            :class="{active: activeFilter === 'in-progress'}"
                            @click="activeFilter = 'in-progress'">
                        进行中
                    </div>
                    <div
                            class="filter-tab"
                            :class="{active: activeFilter === 'completed'}"
                            @click="activeFilter = 'completed'">
                        已完成
                    </div>
                    <div
                            class="filter-tab"
                            :class="{active: activeFilter === 'favorites'}"
                            @click="activeFilter = 'favorites'">
                        收藏
                    </div>
                </div>
            </div>

            <!-- 课程列表 -->
            <div class="courses-container">
                <template v-if="filteredCourses.length > 0">
                    <div
                            class="course-card fade-in"
                            v-for="(course, index) in filteredCourses"
                            :key="index">
                        <div class="course-header" :style="{background: course.color}">
                            <div class="course-tag">{{ course.status }}</div>
                        </div>
                        <div class="course-body">
                            <div class="course-title">
                                <span>{{ course.title }}</span>
                                <i
                                        class="el-icon-star-on"
                                        :style="{color: course.isFavorite ? '#f7b500' : '#dcdfe6', cursor: 'pointer'}"
                                        @click="toggleFavorite(course)">
                                </i>
                            </div>
                            <div class="course-instructor">
                                教师: 王教授
                            </div>

                            <div class="course-progress" v-if="course.progress > 0">
                                <div class="progress-label">
                                    <span>学习进度</span>
                                    <span>{{ course.progress }}%</span>
                                </div>
                                <div class="progress-bar">
                                    <div class="progress-fill" :style="{width: course.progress + '%'}"></div>
                                </div>
                            </div>

                            <div class="course-stats">
                                <span><i class="el-icon-video-camera"></i> {{ course.lessons }}课时</span>
                                <span><i class="el-icon-s-claim"></i> {{ course.assignments }}作业</span>
                            </div>

                            <div class="course-actions">
                                <button class="action-btn" @click="viewCourse(course)">
                                    <i class="el-icon-view action-icon"></i>
                                    查看
                                </button>
                                <button
                                        class="action-btn"
                                        :class="{'primary': course.progress === 0}"
                                        @click="continueCourse(course)">
                                    <i class="el-icon-caret-right action-icon"></i>
                                    {{ course.progress > 0 ? '继续' : '开始' }}
                                </button>
                            </div>
                        </div>
                    </div>
                </template>

                <div class="empty-state" v-else>
                    <i class="el-icon-notebook-1 empty-icon"></i>
                    <div class="empty-text">没有找到相关课程</div>
                    <el-button type="primary" @click="browseCourses">浏览课程</el-button>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
    new Vue({
        el: '#app',
        data: {
            searchQuery: '',
            activeFilter: 'all',
            courses: [] // 改为由API获取
        },
        mounted() {
            fetch('/courses', {
                headers: {
                    'Authorization': 'Bearer ' + localStorage.getItem('token')
                }
            })
            .then(res => res.json())
            .then(resp => {
                if (resp.success && Array.isArray(resp.data)) {
                    // 为API数据添加样式属性，保持原有的美观效果
                    this.courses = resp.data.map((course, index) => ({
                        ...course,
                        // 映射字段名
                        title: course.name,
                        instructor: course.teacher ? course.teacher.name : '未知教师',
                        // 添加渐变色背景
                        color: this.getCourseColor(index),
                        // 添加收藏状态（默认false）
                        isFavorite: course.isFavorite || false,
                        // 确保进度字段存在
                        progress: course.progress || 0,
                        // 确保状态字段存在
                        status: course.status || this.getCourseStatus(course.progress || 0),
                        // 添加课时和作业数量（模拟数据）
                        lessons: course.lessons || Math.floor(Math.random() * 20) + 10,
                        assignments: course.assignments || Math.floor(Math.random() * 5) + 2
                    }));
                } else {
                    console.error('获取课程数据失败:', resp.message);
                    this.courses = [];
                }
            })
            .catch(error => {
                console.error('请求课程数据出错:', error);
                this.courses = [];
            });
        },
        computed: {
            filteredCourses() {
                let filtered = this.courses;

                // 根据搜索查询过滤
                if (this.searchQuery) {
                    const query = this.searchQuery.toLowerCase();
                    filtered = filtered.filter(course =>
                        course.title.toLowerCase().includes(query) ||
                        course.instructor.toLowerCase().includes(query)
                    );
                }

                // 根据活动过滤器过滤
                switch (this.activeFilter) {
                    case 'in-progress':
                        filtered = filtered.filter(course =>
                            course.progress > 0 && course.progress < 100
                        );
                        break;
                    case 'completed':
                        filtered = filtered.filter(course =>
                            course.progress === 100
                        );
                        break;
                    case 'favorites':
                        filtered = filtered.filter(course =>
                            course.isFavorite
                        );
                        break;
                    default:
                        // 'all' 不进行过滤
                        break;
                }

                return filtered;
            }
        },
        methods: {
            toggleFavorite(course) {
                course.isFavorite = !course.isFavorite;
                this.$message({
                    message: course.isFavorite ? '已添加到收藏' : '已从收藏移除',
                    type: 'success'
                });
            },
            viewCourse(course) {
                this.$message(`正在查看课程: ${course.title}`);
                // 实际应用中这里会跳转到课程详情页
            },
            continueCourse(course) {
                this.$message(`继续学习课程: ${course.title}`);
                // 实际应用中这里会跳转到课程学习页面
            },
            browseCourses() {
                this.$message('浏览更多课程');
                // 实际应用中这里会跳转到课程目录页
            },
            // 根据索引生成课程颜色
            getCourseColor(index) {
                const colors = [
                    'linear-gradient(135deg, #4361ee, #3f37c9)',
                    'linear-gradient(135deg, #f72585, #4895ef)',
                    'linear-gradient(135deg, #4895ef, #2ec4b6)',
                    'linear-gradient(135deg, #2ec4b6, #e71d36)'
                ];
                return colors[index % colors.length];
            },
            // 根据进度生成状态
            getCourseStatus(progress) {
                if (progress === 0) {
                    return '未开始';
                } else if (progress === 100) {
                    return '已完成';
                } else {
                    return '进行中';
                }
            }
        }
    });
</script>
<script>
  fetch('sidebar.html').then(r=>r.text()).then(html=>{document.getElementById('sidebar').innerHTML=html;});
</script>
</body>
</html>